<?php
	//include '../common.php';

	$a = $_GET['a'];
	$id = $_GET['id'];
	//判断一下包含的common文件！！！蛋疼的问题啊，meta标签跳转会闪下屏幕，header跳转前又不能有输出，判断下包含的头文件
	if($a == 'dis'){
		include '../common.php';
	}else{
		include '../admin_head.php';
	}

	//添加分类
	switch($a){
		//添加操作
		case 'add':
			$tmp = $_GET['list'];
			$pid = $_POST['pid'];
			$name = trim($_POST['name']);
			//拼接path
			$sql = "select path,id from ".PRE."category where id={$pid}";
			$list=mysql_fetch_assoc(mysql_query($sql));
			$path = @join('',$list).',';
			if(strlen($path)<=1) $path = '0,';

			//判断条件
			if(empty($name)) exit(admin_jump('类别名不能为空','3','index.php'));
			$sql = "insert into ".PRE."category(pid,name,path) values('{$pid}','{$name}','{$path}')";
			$result = mysql_query($sql);
			if($result && mysql_insert_id($link)>0){
				echo admin_jump('添加成功','3','index.php?id='.$pid.'&list='.$tmp);
			}else{
				echo admin_jump('添加失败','1000','index.php');
			}
			break;
		case 'dis':
			$id = $_GET['id'];
			$dis = $_GET['dis'];
			$sql = "update ".PRE."category set display='{$dis}' where id='{$id}'";
			$result = mysql_query($sql);

			header('Location:'.$_SERVER['HTTP_REFERER']);
			//echo '<meta http-equiv="Refresh" content="0; url='.$_SERVER['HTTP_REFERER'].'"/>';

			break;
		case 'edit':
			$name = $_POST['name'];
			$id = $_POST['id'];
			$pid = $_POST['pid'];
			$list = $_GET['list'];

			$sql = "update ".PRE."category set name='{$name}' where id='{$id}'";
			mysql_query($sql);

			echo admin_jump('修改成功','3','index.php?id='.$pid.'&list='.$list);
			break;
			
		case 'del':
			$id = $_GET['id'];
			$pid = $_GET['pid'];
			$list = $_GET['list'];
			
			//判断是否为空
			$sql = "select id from ".PRE."category where pid='{$id}'";
			$result = mysql_query($sql);
			if($result && mysql_affected_rows()>0){
				echo admin_jump('包含子分类，不能删除',3,'index.php?id='.$pid.'&list='.$list);
				exit;
			}
			
			$sql = "select id from ".PRE."goods where cate_id='{$id}'";
			$result = mysql_query($sql);
			if($result && mysql_affected_rows()>0){
				echo admin_jump('包含商品，不能删除',3,'index.php?id='.$pid.'&list='.$list);
				exit;
			}
			
			$sql = "delete from ".PRE."category where id='{$id}'";
			$result = mysql_query($sql);
			if($result && mysql_affected_rows()>0){
				echo admin_jump('删除成功',3,'index.php?id='.$pid.'&list='.$list);
			}else{
				echo admin_jump('删除失败',3,'index.php?id='.$pid.'&list='.$list);
			}

			
			break;
	
	
	}
	
	
include '../admin_footer.php';
?>
